package com.java.array;

public class ArrayDemo {
    public static void main(String[] args) {

        //定义数组
        int[] array = new int[5];
        System.out.println("数组长度为"+array.length);
        //System.out.println("默认值为："+ array[0]);
        //静态初始化
        int[] array1 = new int[]{1,2,3,4,5};
        System.out.println("静态初始化后的数组为："+array1[2]);

        //定义数组
        String[] names = {"张三", "李四", "王五","刘备","张飞","关羽"};
        //遍历输出
        for(int i = 0; i < names.length; i++){
            System.out.println("人物名字为:" + names[i]);
        }

        for(String name : names){
            System.out.println("人物名字为:" + name);
        }
        //二维数组
        int[][] array2 = new int[3][4];
        //给第二行的第二列赋值
        array2[1][1] = 99;
        System.out.println("二维数组第二行第二列的值为："+array2[1][1]);
        //访问行长度
        System.out.println("行长度为："+array2.length);
        //访问列长度
        System.out.println("列长度为："+array2[0].length);

        int[][] array3 = {{1,2,3},{4,2,6,10},{9,4,4,8}};
        //常规的for循环
        for(int i = 0; i < array3.length; i++){
            for(int j = 0; j < array3[i].length; j++){
                System.out.print(array3[i][j]+" ");
            }
            System.out.println();
        }

        //foreach循环
        for(int[] row : array3){
            for(int data : row){
                System.out.print(data+" ");
            }
            System.out.println();
        }
    }
}
